package com.cares.core.mng.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cares.core.base.controller.BaseController;
import com.cares.core.mng.service.MenuService;
import com.cares.entity.mng.Menu;
import com.cares.entity.mng.MenuOperate;

import com.alibaba.fastjson.JSON;

/**
 * LoginController负责打开登录页面(GET请求)和登录出错页面(POST请求)，
 * 
 * 真正登录的POST请求由Filter完成,
 * 
 * @author calvin
 */
@Controller
@RequestMapping(value = "/Mng/Menu")
public class MenuController extends BaseController {

	@Autowired
	@Qualifier("menuService")
	MenuService menuService;

	private Map<String, String> map = new HashMap<String, String>();
	
	/**
	 * GET方法，菜单主页面加载
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String mainPage() {

		return "frm/mng/menu/menuTreeGrid";
	}

	/**
	 * 新增或更新菜单信息
	 */
	@RequestMapping(value = "updateMenu")
	@ResponseBody
	public void updateMenu(Menu menu, HttpServletRequest req,
			HttpServletResponse response) {
		try {
			map.put("STATE", "SUCCESS");
			menuService.saveOrUpdateMenu(menu);
		} catch (Exception ex) {
			map.put("STATE", "ERROR");
			map.put("INFO", ex.getMessage());

		}
		super.outString(response, JSON.toJSONString(map));
	}

	/**
	 * 逻辑删除菜单及子菜单
	 */
	@RequestMapping(value = "disMenu")
	@ResponseBody
	public void disMenu(Menu menu, HttpServletRequest req,
			HttpServletResponse response) {
		try {
			map.put("STATE", "SUCCESS");
			menuService.deleteMenu(menu.getId());
		} catch (Exception ex) {
			map.put("STATE", "ERROR");
			// map.put("INFO", ex.getMessage());

		}
		super.outString(response, JSON.toJSONString(map));
	}

	/**
	 * 查询菜单列表信息
	 */
	@RequestMapping(value = "treeGridData")
	@ResponseBody
	public void treeGridData(Menu menu, HttpServletRequest req,
			HttpServletResponse response) {
		Long nodeId = new Long("1");
		List<Map<String, Object>> itemsList = menuService.menuTree(nodeId);

		String json = JSON.toJSONString(itemsList);
		super.outString(response, json);

	}
	
	/**
	 * 角色菜单关联，获取菜单信息
	 */
	@RequestMapping(value = "treeData")
	@ResponseBody
	public void treeData(Menu menu, HttpServletRequest req,
			HttpServletResponse response) {
		Long nodeId = new Long("1");
		// XXX 此处不应获取所有菜单信息，应获取当前用户所能查看的菜单信息
		List<Map<String, Object>> itemsList = menuService.menuTree(nodeId,1);

		String json = JSON.toJSONString(itemsList);
		super.outString(response, json);

	}

	/**
	 * 获取上级菜单
	 * PS: 目前只支持2级菜单，因此该方法仅获取一级菜单
	 * @param menu
	 * @param req
	 * @param response
	 */
	@RequestMapping(value = "topMenuData")
	@ResponseBody
	public void topMenuData(Menu menu, HttpServletRequest req,
			HttpServletResponse response) {
		Long nodeId = new Long("1");
		List<Map<String, Object>> itemsList = menuService.menuTopList(nodeId);

		String json = JSON.toJSONString(itemsList);
		super.outString(response, json);

	}

	/**
	 * 新增编辑页面根据menuId获取Menu信息
	 */
	@RequestMapping(value = "detail")
	@ResponseBody
	public void menuDetail(Long mId, HttpServletRequest req,
			HttpServletResponse response) {

		try {
			String json = JSON.toJSONString(menuService.getMenu(mId));
			super.outString(response, json);
		} catch (Exception e) {
			map.put("INFO", e.getMessage());
			super.outString(response, JSON.toJSONString(map));
		}

	}

	/**
	 * 加载菜单操作按钮信息
	 */
	@RequestMapping(value = "operateGridData")
	@ResponseBody
	public void operateGridData(Menu menu, HttpServletRequest req,
			HttpServletResponse response) {
		if (null != req.getParameter("menuId")) {
			Long menuId = new Long(req.getParameter("menuId"));
			MenuOperate param = new MenuOperate();
			param.setMenuId(menuId);
			List<MenuOperate> itemsList = menuService.findMenuOperate(param);

			String json = JSON.toJSONString(itemsList);
			super.outString(response, json);
		}

	}

	/**
	 * 新增或更新菜单操作按钮信息
	 */
	@RequestMapping(value = "updateOperate")
	@ResponseBody
	public void updateOperate(MenuOperate menuOpt, HttpServletRequest req,
			HttpServletResponse response) {
		try {

			MenuOperate param = new MenuOperate();
			param.setCode(menuOpt.getCode());
			List<MenuOperate> itemsList = menuService.findMenuOperate(param);
			if (null == itemsList || itemsList.size() == 0) {
				menuService.saveOrUpdateMenuOperate(menuOpt);
				map.put("STATE", "SUCCESS");
			} else {
				map.put("INFO", "代码重复！");
				map.put("STATE", "NONE");
			}

		} catch (Exception ex) {
			map.put("STATE", "ERROR");
			map.put("INFO", ex.getMessage());
			ex.printStackTrace();
			// 

		}
		super.outString(response, JSON.toJSONString(map));
	}

	/**
	 * 删除菜单操作按钮信息
	 */
	@RequestMapping(value = "deleteMenuOperate")
	@ResponseBody
	public void deleteMenuOperate(Long optId, HttpServletRequest req,
			HttpServletResponse response) {
		try {
			if (null != optId) {
				map.put("STATE", "SUCCESS");
				menuService.deleteMenuOperate(optId);
			} else {
				map.put("STATE", "NONE");
			}
		} catch (Exception ex) {
			map.put("STATE", "ERROR");
			map.put("MSG", ex.getMessage());
			ex.printStackTrace();
		}
		super.outString(response, JSON.toJSONString(map));
	}

}
